package com.turing.data.scripts.contract.filter

import com.turing.common.util.Log
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.CombineLongText
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO

/**
 * 场景：资产管理合同
 * 版本：Version 1
 * 要素名称：是否聘请投资顾问
 */

class IfInvestmentConsultant_30208_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def Regex_HAS_1 = /.*投资顾问.*/
        def Regex_NOT_HAS_1 = /.*不.*投资顾问.*/
        def Regex_NOT_HAS_2 = /.*未.*投资顾问.*/
        def Regex_NOT_HAS_3 = /.*没.*投资顾问.*/

        def textPathMap = dependParams.textPathMap

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        def REGEX_BACKUP = "投资顾问"
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                def drawResultVO = resultVOs.get(i)
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 40 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,start)
                    if( expandString =~ REGEX_BACKUP ){
                        trueValueSet.add(drawResultVO)
                        return trueValueSet
                    }
                    expandOffset ++
                }
            }
        }

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,30)
                Log.info("IfInvestmentConsultant_9788_11_filter_resultVOs2={}",resultVOs.get(i))
                Log.info("IfInvestmentConsultant_9788_11_filter_trueValue2={}",trueValue)

                if ( trueValue =~ Regex_HAS_1 ){
                    trueValueSet.add(resultVOs.get(i))
                    return trueValueSet
                }
            }
        }

        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
//                    return  resultVOs
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,30)
                Log.info("IfInvestmentConsultant_9788_11_filter_resultVOs1={}",resultVOs.get(i))
                Log.info("IfInvestmentConsultant_9788_11_filter_trueValue1={}",trueValue)

                if ( (trueValue =~ Regex_NOT_HAS_1 || trueValue =~ Regex_NOT_HAS_2 || trueValue =~ Regex_NOT_HAS_3) ){
                    trueValueSet.add(resultVOs.get(i))
                    return trueValueSet
                }
            }
        }

        return  resultVOs
    }



}
